package com.pt.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pt.common.CommonResult;
import com.pt.entity.PrivateMessage;
import com.pt.entity.User;
import com.pt.service.PrivateMessageService;
import com.pt.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Api(tags = "私信管理")
@RestController
@RequestMapping("/api/messages")
@RequiredArgsConstructor
public class PrivateMessageController {
    
    private final PrivateMessageService privateMessageService;
    private final UserService userService;
    
    @ApiOperation("获取收件箱")
    @GetMapping("/inbox")
    @PreAuthorize("hasRole('USER')")
    public CommonResult<IPage<PrivateMessage>> getInbox(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页数量") @RequestParam(defaultValue = "20") Integer size,
            HttpServletRequest request) {
        
        User user = userService.getCurrentUser();
        
        Page<PrivateMessage> pageParam = new Page<>(page, size);
        IPage<PrivateMessage> messages = privateMessageService.getInbox(user.getId(), pageParam);
        return CommonResult.success(messages);
    }
    
    @ApiOperation("获取发件箱")
    @GetMapping("/outbox")
    @PreAuthorize("hasRole('USER')")
    public CommonResult<IPage<PrivateMessage>> getOutbox(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页数量") @RequestParam(defaultValue = "20") Integer size,
            HttpServletRequest request) {
        
        User user = userService.getCurrentUser();
        
        Page<PrivateMessage> pageParam = new Page<>(page, size);
        IPage<PrivateMessage> messages = privateMessageService.getOutbox(user.getId(), pageParam);
        return CommonResult.success(messages);
    }
    
    @ApiOperation("发送私信")
    @PostMapping("/send")
    @PreAuthorize("hasRole('USER')")
    public CommonResult<PrivateMessage> sendMessage(@RequestBody PrivateMessage message, HttpServletRequest request) {
        User user = userService.getCurrentUser();
        
        message.setSenderId(user.getId());
        message.setSentTime(LocalDateTime.now());
        message.setIsRead(false);
        message.setSenderDeleted(false);
        message.setReceiverDeleted(false);
        
        PrivateMessage sentMessage = privateMessageService.sendMessage(message);
        return CommonResult.success(sentMessage);
    }
    
    @ApiOperation("标记为已读")
    @PutMapping("/{messageId}/read")
    @PreAuthorize("hasRole('USER')")
    public CommonResult<String> markAsRead(@ApiParam("消息ID") @PathVariable Long messageId, HttpServletRequest request) {
        User user = userService.getCurrentUser();
        
        privateMessageService.markAsRead(messageId, user.getId());
        return CommonResult.success("标记成功");
    }
    
    @ApiOperation("删除私信")
    @DeleteMapping("/{messageId}")
    @PreAuthorize("hasRole('USER')")
    public CommonResult<String> deleteMessage(@ApiParam("消息ID") @PathVariable Long messageId, HttpServletRequest request) {
        User user = userService.getCurrentUser();
        
        privateMessageService.deleteMessage(messageId, user.getId());
        return CommonResult.success("删除成功");
    }
    
    @ApiOperation("获取未读消息数量")
    @GetMapping("/unread-count")
    @PreAuthorize("hasRole('USER')")
    public CommonResult<Integer> getUnreadCount(HttpServletRequest request) {
        User user = userService.getCurrentUser();
        
        Integer count = privateMessageService.getUnreadCount(user.getId());
        return CommonResult.success(count);
    }
} 